package org.east.common.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.east.common.exception.ApplicationException;

/**
 * 时间工具
 * @since 20160513
 * @author fund9
 *
 */
public class TimeUtils {
	
	/**
	 * 形如：yyyy-MM-dd 转成Date类型
	 * @param date
	 * @return
	 * @throws RuntimeException
	 */
	public static Date parse1(String date) throws RuntimeException{
		Date d = null;  
        try {
			d = new SimpleDateFormat("yyyy-MM-dd").parse(date);
		} catch (ParseException e) {
			throw new ApplicationException(e.getMessage(),e);
		}
        return d;
	}
	
	/**
	 * 日期以形如：yyyy-MM-dd 输出
	 * @param date
	 * @return 形如：yyyy-MM-dd
	 */
	public static String format1(Date date){
		return new SimpleDateFormat("yyyy-MM-dd").format(date);
	}

	/**
	 * 加上或减去n天
	 * @param date
	 * @param n
	 * @return
	 */
	public static Date addDay(Date date,int n) {
		Calendar c = Calendar.getInstance();
        c.setTime(date);
        /*int tday = c.get(Calendar.DATE);  
        c.set(Calendar.DATE, tday - 1);*/
        //c.add(Calendar.DATE, n);
        c.add(Calendar.DAY_OF_MONTH, n);
        
        return c.getTime();
	}
	
	/*public static void main(String[] arges){
		System.out.println(format1(addDay(parse1("2016-05-01"),-1)));
		System.out.println(format1(addDay(parse1("2016-03-01"),-1)));
		System.out.println(format1(addDay(parse1("2016-01-01"),-1)));
		System.out.println(format1(addDay(parse1("2016-01-01"),-2)));
		System.out.println(format1(addDay(parse1("2016-02-29"),1)));
		System.out.println(format1(addDay(parse1("2016-02-29"),90)));
	}*/
	
	
}
